package com.utc.cards.player.view;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.utc.cards.Constants;
import com.utc.cards.R;
import com.utc.cards.model.PlayerModel;
import com.utc.cards.model.game.InfoType;
import com.utc.cards.player.jade.PlayerAgentManager;
import com.utc.cards.player.jade.agent.playerAgent.IPlayerAgent;
import com.utc.cards.utils.Utils;
//import com.utc.cards.player.jade.PlayerAgentManager;
//import com.utc.cards.player.jade.agent.playerAgent.IPlayerAgent;
//import com.utc.cards.player.jade.agent.playerHelperAgent.IPlayerHelperAgent;

public class PlayerMenuActivity extends Activity
{
    private static Logger log = LoggerFactory.getLogger(PlayerMenuActivity.class);

    // private Handler mHandler = new Handler();
    private IPlayerAgent playerAgent;
    // private IPlayerHelperAgent playerHelperAgent;

    private MyReceiver myReceiver;
    private ListView playerList;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_player_menu);

        loadPlayerAgent();

        registerReceivers();

        // showJoinButton();

        // SharedPreferences settings = getSharedPreferences(
        // JADE_CARDS_PREFS_FILE, Context.MODE_PRIVATE);
        // String gmailAddress = settings.getString(GMAIL, "");

        // TextView googleAccountTextView = (TextView)
        // findViewById(R.id.googleAccountTextView);
        // googleAccountTextView.setText("Google Account : " + gmailAddress);
    }

    private void registerReceivers()
    {
        myReceiver = new MyReceiver();

        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.SHOW_GAME);
        filter.addAction(Constants.POP_INFO);
        filter.addAction(Constants.PLAYER_LIST);
        filter.addAction(Constants.GAME_NAME);
        filter.addAction(Constants.LOCK_CONNEXION);
        registerReceiver(myReceiver, filter);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(myReceiver);
        log.debug("Destroy activity!");
    }

    private class MyReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            log.info("Received intent " + action);

            if (action.equalsIgnoreCase(Constants.SHOW_GAME))
            { // lance
              // l'activity de
              // jeu
                IPlayerGameActivity activity = PlayerModel.Instance().getGame().createPlayerGameActivity();
                Intent showGame = new Intent(PlayerMenuActivity.this, activity.getClass());
                PlayerMenuActivity.this.startActivity(showGame);

            }
            else if (action.equalsIgnoreCase(Constants.POP_INFO))
            {
                // afficher une info
                Utils.showAlertDialog(PlayerMenuActivity.this, intent.getStringExtra(InfoType.INFO.name()), false);

            }
            else if (action.equalsIgnoreCase(Constants.PLAYER_LIST))
            {
                String[] players = intent.getStringArrayExtra(Constants.PLAYER_LIST);
                updatePlayerList(players);
            }
            else if (action.equalsIgnoreCase(Constants.GAME_NAME))
            {
                // met à jour le nom du jeu
                TextView gameName = (TextView) findViewById(R.id.gameNamePlayerMenu);
                gameName.setText(intent.getStringExtra(Constants.GAME_NAME));
            }
            else if (action.equalsIgnoreCase(Constants.LOCK_CONNEXION))
            {
                // hideJoinButton();
            }

        }
    }

    private void updatePlayerList(String[] players)
    {
        if (playerList == null)
        {
            playerList = (ListView) findViewById(R.id.playerList);

            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) playerList.getLayoutParams();

            lp.height = (int) (Utils.getDisplayWindowsDimention(this).y * 0.3);

            playerList.setLayoutParams(lp);
        }

        playerList.setAdapter(new ArrayAdapter<String>(PlayerMenuActivity.this, R.layout.centered_list_row, R.id.textItem, players));
    }

    public void join(View v)
    {
        System.out.println("Join game ! : " + playerAgent == null);

        playerAgent.joinHostGame();

        // hideJoinButton();
    }

    // private void showJoinButton()
    // {
    // Button bt = (Button) findViewById(R.id.joinButton);
    // bt.setVisibility(View.VISIBLE);
    //
    // ProgressBar pb = (ProgressBar) findViewById(R.id.waitingOtherPlayers);
    // pb.setVisibility(View.INVISIBLE);
    // }
    //
    // private void hideJoinButton()
    // {
    // Button bt = (Button) findViewById(R.id.joinButton);
    // bt.setVisibility(View.INVISIBLE);
    //
    // ProgressBar pb = (ProgressBar) findViewById(R.id.waitingOtherPlayers);
    // pb.setVisibility(View.VISIBLE);
    // }

    private void loadPlayerAgent()
    {
        SharedPreferences settings = getSharedPreferences(Constants.JADE_CARDS_PREFS_FILE, Context.MODE_PRIVATE);
        playerAgent = PlayerAgentManager.instance().getAgent(this, Constants.CARDS_PLAYER_AGENT_NAME + "-" + settings.getString(Constants.GMAIL, ""), IPlayerAgent.class);
        log.debug("playerAgent loaded !");
    }
    // private void loadPlayerHelperAgent()
    // {
    // SharedPreferences settings = getSharedPreferences(JADE_CARDS_PREFS_FILE, Context.MODE_PRIVATE);
    // playerHelperAgent = PlayerAgentManager.instance().getAgent(this, Constants.CARDS_PLAYER_HELPER_AGENT_NAME + "-" + settings.getString(GMAIL, ""), IPlayerHelperAgent.class);
    // log.debug("playerAgent loaded !");
    // }
}
